/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatraz.server;

import alcatraz.common.IAlcatraz;
import java.net.UnknownHostException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Logger;
import java.rmi.server.RemoteServer;


/**
 * Starten des Server mit RMI Funktion
 * @author Christian Pipp
 */
public class ServerRunner implements IAlcatraz {

   /**
    * Main Function des Servers
    * @author Christian Pipp
    * @param String[] args
    */
   public static void main(String[] args)
   {
        ServerRunner server = new ServerRunner();

        try  {
        server.startServer();
        } catch (UnknownHostException ue) {
            System.out.println("RemoteException -> Following Error occured: ".concat(ue.getMessage()));
        } catch (RemoteException re) {
            System.out.println("RemoteException -> Following Error occured: ".concat(re.getMessage()));
        }
        
   }

   /**
    * RMI Server wird hier registriert und gestartet
    * @author christian
    */

   public void startServer() throws RemoteException, UnknownHostException   {
        Registry registry = null;

        Logger.getLogger("Starting server...");
        System.out.println("Starting server...");

        LocateRegistry.createRegistry(Registry.REGISTRY_PORT);

        AlcatrazServerImpl server = new AlcatrazServerImpl("localhost");
        
        RemoteServer.setLog(System.out);
        registry = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);

        System.out.println("Trying rebind to RMI...");
        registry.rebind("rmi://"
                .concat(Host)
                .concat("/")
                .concat(RMIServerName)
                , (Remote) server);

        server.joinSpreadGroup(SpreadGroupName);
        //server.setServerIdle();
   }
}
